#!/usr/bin/env python3
"""
测试admin登录功能
"""
import sys
import os
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

from sqlalchemy.orm import Session
from core.database import SessionLocal
from models.database import User
from core.auth import verify_password

def test_admin_login():
    """测试admin登录"""
    username = "admin"
    password = "A1##B2"
    
    try:
        db = SessionLocal()
        admin_user = db.query(User).filter(User.username == username).first()
        
        if not admin_user:
            print(f"❌ 未找到用户: {username}")
            return False
        
        print(f"🔍 找到用户: {username}")
        print(f"   角色: {admin_user.role}")
        print(f"   状态: {'活跃' if admin_user.is_active else '禁用'}")
        
        # 测试密码验证
        is_valid = verify_password(password, admin_user.password_hash, client_hashed=False)
        print(f"   密码验证: {'✅ 成功' if is_valid else '❌ 失败'}")
        
        if is_valid:
            print(f"\n🎉 登录测试成功！")
            print(f"   用户名: {username}")
            print(f"   密码: {password}")
            print(f"   可以使用这些凭据登录系统")
            print(f"\n💡 现在可以启动服务并登录了:")
            print(f"   python main.py")
            print(f"   然后访问: http://localhost:8000/web")
        else:
            print(f"\n❌ 登录测试失败！")
            print(f"   请检查密码哈希算法")
        
        return is_valid
        
    except Exception as e:
        print(f"❌ 测试登录时发生错误: {e}")
        return False
    finally:
        db.close()

def show_login_info():
    """显示登录信息"""
    print("🔐 Admin登录信息")
    print("=" * 40)
    print("用户名: admin")
    print("密码: A1##B2")
    print("")
    print("🌐 访问地址:")
    print("   http://localhost:8000/web")
    print("")
    print("📋 启动命令:")
    print("   python main.py")
    print("")
    print("⚠️  注意:")
    print("   - 此密码不符合新的密码强度要求")
    print("   - 建议登录后立即修改密码")
    print("   - 确保服务在端口8000上运行")

if __name__ == "__main__":
    print("🔐 Admin登录测试")
    print("=" * 40)
    
    if test_admin_login():
        print("\n" + "=" * 60)
        show_login_info()
    else:
        print("\n❌ 登录测试失败，请检查系统配置")